#define PSF2_MAGIC0     0x72
#define PSF2_MAGIC1     0xb5
#define PSF2_MAGIC2     0x4a
#define PSF2_MAGIC3     0x86

/* bits used in flags */
#define PSF2_HAS_UNICODE_TABLE 0x01

/* max version recognized so far */
#define PSF2_MAXVERSION 0

/* UTF8 separators */
#define PSF2_SEPARATOR  0xFF
#define PSF2_STARTSEQ   0xFE

typedef struct{
        unsigned char magic[4];
        unsigned int version;
        unsigned int headersize;    /* offset of bitmaps in file */
        unsigned int flags;
        unsigned int length;        /* number of glyphs */
        unsigned int charsize;      /* number of bytes for each character */
        unsigned int height, width; /* max dimensions of glyphs */
        /* charsize = height * ((width + 7) / 8) */
} psf2_header;

int LoadPSF2Font(int f,unsigned int *Width,int *CharSize){
  unsigned int err;
  static psf2_header font_hdr;
  lseek(f,0, 0,& err,& err);
  fread(f,&font_hdr,sizeof(psf2_header),&err);
  if ((font_hdr.magic[0]==PSF2_MAGIC0)&&
      (font_hdr.magic[1]==PSF2_MAGIC1)&&
      (font_hdr.magic[2]==PSF2_MAGIC2)&&
      (font_hdr.magic[3]==PSF2_MAGIC3)){
          fontbitmap = malloc(font_hdr.charsize*font_hdr.length);
          lseek(f,font_hdr.headersize,0,& err,& err);
          fread(f,fontbitmap,font_hdr.charsize*font_hdr.length,&err);
          *Width = font_hdr.width;
          *CharSize = font_hdr.charsize;
          return(font_hdr.height);
   }
   return(0);
}
